# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql

class ZhiderPipeline(object):

    def process_item(self, item, spider):
        if(spider.name == "Topic" or spider.name=="DetailTopic"):
            print("spider name %s"%spider.name)
            id = item["id"]
            title = item["title"]
            pid = item["pid"]
            db = pymysql.connect(host="localhost",user="python",password="python",db="zhider",port=3306,charset="utf8")
            try:
                cur = db.cursor()
                cur.execute("insert into topic (id, title, pid) values ('%s', '%s', '%s')"%(id,title,pid))
                db.commit()
            except Exception as ex:
                print(ex)
                db.rollback()
            finally:
                db.close()

        if(spider.name == "Question") :
            print("get question")
            tid = item["topicid"]
            text = item["text"]
            url = item["url"]
            print ("%s %s %s"%(tid,url,text))

